<?php

require_once '../../ConfigurationFactory.php';

class DaoFactory {

    private static $connect = null;
    private static $config = null;

    public function  __construct() {
        if(self::$config == null)
            self::$config = ConfigurationFactory::getInstance();
    }

    public function apriConnessione() {
        $error = "errore connessione  ";

        //echo ("<br>".self::$config->host."<br>".self::$config->user."<br>".self::$config->password)."<br>";

        if(self::$connect == null && self::$config->mySqlAccesDatabase) {
            
            $esito = null;

            self::$connect = mysql_connect(self::$config->host, self::$config->user, self::$config->password);

            if (self::$connect != null) {
                $esito = mysql_select_db(self::$config->db);
                
                $error = "errore DB ";

            }

            if (!$esito || !self::$connect) {
                self::$connect = null;
                die($error . mysql_error());

            }

        }

        if(self::$connect == null && self::$config->PDOAccesDatabase) {

            $connectString = "mysql:host=".self::$config->host.";dbname=".self::$config->db;

            try
                {
                   self::$connect = new PDO($connectString, self::$config->user, self::$config->password   );
                   self::$connect->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
                   self::$connect->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

                }
                catch(PDOException $exc)
                {
                    var_dump($exc);
                    $error = $error.E_USER_ERROR;
                    trigger_error('NO PDO Connection', E_USER_ERROR);
                }

        }

        return self::$connect;
    }

    public function chiudiConnessione() {
        if(self::$config->mySqlAccesDatabase)
            mysql_close();
        
    }

    /**
     * Se la transazione prevede ci siano più query prima del commit, l'oggetto $myConnect dovrà essere valorizzato.
     * In caso contrario verrà valorizzato in maniera autonoma
     * @param <type> $query
     * @param <type> $myConnect
     * @return <type>
     */
    public function eseguiQuery($query, $myConnect=null) {

        $rs = null;

        $transazione = ($myConnect != null);

        if(!$transazione)
            $myConnect = $this->apriConnessione();

        if(self::$config->mySqlAccesDatabase) {

            $rs = mysql_query($query);

            if(!$rs) {
                $error =  NOME_CLASSE.mysql_error();
                die($error);
                }

        }

        if(self::$config->PDOAccesDatabase) {
            
            try {
                if(!$transazione)
                    $myConnect->beginTransaction();

               // $rs = $myConnect->query($query);
                $sqlPrepared = $myConnect->prepare($query);
                $sqlPrepared->execute();
                $rs = $sqlPrepared->fetchAll();

                if(!$transazione)
                    $myConnect->commit();
                
            } catch (PDOException $ex) {
               $error = $ex->getMessage();
               $errorArray = self::$connect->errorInfo();
               $error .= " - ".$errorArray[2];
            
            }
        
        }

        if(!$transazione)
                $this->chiudiConnessione();
        
        return $rs;

    }

}

//$test = new DaoFactory();
//$test->apriConnessione();
//var_dump($test->eseguiQuery("select * from user"));
//$test->chiudiConnessione();

?>